//
//  LanguageListViewModel.swift
//  Chatup
//
//  Created by iron man on 2024/2/27.
//

import Foundation

@MainActor
class LanguageViewModel: ObservableObject {
    @Published var uniqueLanguageNames: [String: String]
    @Published var currentName: String = ""
    @Published var data: [String] = []

    init() {
        self.currentName = ""
        self.uniqueLanguageNames = [:]
        // 当前设备设置的语言环境
        let currentLocale = Locale.current
        // 获取设备支持的语言环境
        let availableLocales = Locale.availableIdentifiers.map { Locale(identifier: $0) }
        // 直接使用currentLocale的languageCode

        
       
        self.currentName = currentLocale.localizedString(forLanguageCode: currentLocale.languageCode!) ?? ""
        

        // 填充uniqueLanguageNames
        for locale in availableLocales {
            self.uniqueLanguageNames[locale.languageCode ?? ""] = currentLocale.localizedString(forLanguageCode: locale.languageCode ?? "") ?? ""
        }
        
        let sort = self.uniqueLanguageNames.sorted { $0.value < $1.value }
        for (code,name) in sort{
            print("code:\(code) name: \(name)")
            self.data.append(name)
        }
    }
}
